package com.hncu.edu.info.manager.dao;

import com.hncu.edu.info.manager.domain.Teacher;

import java.util.ArrayList;

public class OtherTeacherDao implements BaseTeacherDao{
    //创建集合
    static ArrayList<Teacher> arr = new ArrayList<>();

    //静态代码段，便于测试
    static {
        Teacher teacher1 = new Teacher("hncu001", "张三", "40", "1980-10-10");
        Teacher teacher2 = new Teacher("hncu002", "李四", "50", "1970-11-11");
        arr.add(teacher1);
        arr.add(teacher2);
    }

    //空参构造
    public OtherTeacherDao() {

    }

    //传递数组，查看老师
    public Teacher[] findAllTeacher() {
        Teacher[] teachers = new Teacher[arr.size()];
        //将集合中的每一个元素都放至数组中去
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = arr.get(i);
            teachers[i] = teacher;
        }
        return teachers;
    }

    //添加学生
    public boolean addTeacher(Teacher teacher) {
        arr.add(teacher);
        return true;
    }

    //删除学生
    public void deleteTeacher(String deleteId) {
        //根据传来的学号找到相应的索引位置
        int Index = getIndex(deleteId);
        //将其删除
        arr.remove(Index);
    }

    //根据工号找索引位置
    public int getIndex(String Id) {
        //先给索引赋初值，表示不在数组中
        int Index = -1;
        //遍历数组，找到工号所对应的索引位置
        for (int i = 0; i < arr.size(); i++) {
            Teacher teacher = arr.get(i);
            if (teacher != null && teacher.getid().equals(Id)) {
                Index = i;
            }
        }
        return Index;
    }

    //修改老师
    public void updateTeacher(Teacher teacher) {
        //将要修改的Id
        String Id = teacher.getid();
        //根据Id找到位置
        int Index = getIndex(Id);
        //将新的对象填入集合中
        arr.set(Index,teacher);
    }
}
